/**
 *  zw-slider
 *  
 */
;(function($, win, b){
	var getSrc = function(ele){
		if(ele.tagName.toLowerCase() == 'img')
			return ele.src;
		var backgroundImage = $(ele).css('backgroundImage');
		var match = backgroundImage.match(/url\((.*)\)/);
		return match ? match[1] : '';
	};
	$.fn.zwSlide = function(selector, currentEle){
		var selector = selector || '.zw-gp';
		
			var par = $(this);
			var isScrolling, slider, slideWidth, slideNumber, sliderString, containerWidth, containerHeight, marginRight = 15, slidesLength;
			var pageX,pageY,deltaX,deltaY,offsetX,lastSlide,startTime,resistance;

			var transformPrefix = b.cssPrefix;
			var transformProperty = b.transform;
			var getScroll = function(){
				var translate3d = slider[0].style[transformProperty].match(/translate3d\(([^,]*)/);
				var ret = translate3d ? translate3d[1] : 0;
				//console.log(ret);
				return parseInt(ret, 10);
			};

			var setSlideNumber = function (offset) {
		    var round = offset ? (deltaX < 0 ? 'ceil' : 'floor') : 'round';
		    slideNumber = Math[round](getScroll() / (slidesLength / slider.children().length));
		    slideNumber += offset;
		    slideNumber = Math.min(slideNumber, 0);
		    slideNumber = Math.max(-(slider.children().length - 1), slideNumber);
		  };

			var ontouchstart = function (e) {
				e.preventDefault();
		    e.stopPropagation();
		    isScrolling    = undefined;
		    resistance     = 1;
		    lastSlide      = -(slider.children().length - 1);
		    startTime      = +new Date();
		    pageX          = e.touches[0].pageX;
		    pageY          = e.touches[0].pageY;
		    deltaX         = 0;
		    deltaY         = 0;

		    setSlideNumber(0);

		    slider[0].style[transformPrefix + 'transition-duration'] = 0;
		  };
			var ontouchmove = function (e) {
		    if (e.touches.length > 1) {
		      return; // Exit if a pinch || no slider
		    }

		    
		    deltaX = e.touches[0].pageX - pageX;
		    deltaY = e.touches[0].pageY - pageY;
		    pageX  = e.touches[0].pageX;
		    pageY  = e.touches[0].pageY;

		    isScrolling = true;

		    offsetX = (deltaX / resistance) + getScroll();

		    e.preventDefault();

		    resistance = slideNumber === 0         && deltaX > 0 ? (pageX / slideWidth) + 1.25 :
		                 slideNumber === lastSlide && deltaX < 0 ? (Math.abs(pageX) / slideWidth) + 1.25 : 1;

		    slider[0].style[transformProperty] = 'translate3d(' + offsetX + 'px,0,0)';

		    
		  };
			var ontouchend = function (e) {
				if(!isScrolling){
					slider.parent().remove();
					return
				}
		    setSlideNumber((+new Date()) - startTime < 1000 && Math.abs(deltaX) > 15 ? (deltaX < 0 ? -1 : 1) : 0);

		    offsetX = slideNumber * slideWidth;

		    slider[0].style[transformPrefix + 'transition-duration'] = '.2s';
		    slider[0].style[transformProperty] = 'translate3d(' + offsetX + 'px,0,0)';

		  };

					currentEle = $(currentEle);
					var items = par.find(selector);
					var index = items.index(currentEle);

					containerWidth = $('body').width();
					containerHeight = screen.availHeight;
					slideWidth = containerWidth + marginRight;
					slidesLength = slideWidth * items.length;

					sliderString = '<div class="zw-slider-view" style="width:' + containerWidth + 'px;height:100%;position:fixed;left:50%;top:0;background:#000000;overflow:hidden;margin-left:-' + containerWidth / 2 + 'px"><div class="zw-slider-group" style="position:absolute;left:0;top:0;width:' + slidesLength + 'px;height:'+ containerHeight + 'px;">';
					items.each(function(){
						var item = $(this);
						sliderString += '<div style=\'width:' + containerWidth + 'px;margin-right:' + marginRight + 'px;height:'+ containerHeight + 'px;float:left;background-repeat:no-repeat;background-position:center;background-size:contain;background-image:url(' + getSrc(this) + ');\' ></div>';
					});
					sliderString += '</div></div>';
					var sliderView = $(sliderString);
					slider = sliderView.children();
					var curPos = -(index * slideWidth);
					slider[0].style[transformProperty] = 'translate3d(' + curPos + 'px,0,0)';
					$('body').append(sliderView);

					sliderView[0].addEventListener('touchmove', ontouchmove);
					sliderView[0].addEventListener('touchstart', ontouchstart);
					sliderView[0].addEventListener('touchend', ontouchend);
					sliderView[0].addEventListener('touchcancel', ontouchend);
					if(!window.ontouchstart){
						sliderView.on('tap', function(){
							slider.parent().remove();	
						});	
					}
	}
})(jQuery, window, Browser);

/**
 * zwlazyload
 *
 */
;(function($, win){
	
	$.fn.zwLazyload = function(selector, nobind){
		selector = selector ? selector : '[data-src]';
		this.each(function(){
			var container = $(this);
			var loads;
			var onscroll = function(){
				if(container.is(':hidden')){
					return	
				}
				loads = container.find(selector);
				if(loads.length > 0){
					var viewHeight = $(win).height();
					var scrollTop = $(win).scrollTop();
					loads.each(function(idx, ele){
						ele = $(ele);
						var offsetTop = ele.offset()['top'];
						var offsetBottom = offsetTop + ele.height();
						if( (offsetTop - scrollTop > 0 && offsetTop - scrollTop < viewHeight) ||
							(offsetBottom - scrollTop > 0 && offsetBottom - scrollTop < viewHeight)
							){
							var url = ele.attr('data-src');
							if(this.tagName.toLowerCase() == 'img'){
								ele.attr('src', url);
							}else{
								ele.css('backgroundImage', 'url(' + url + ')');	
							}
							ele.removeAttr('data-src');
						}
					});
				}
			};
			if(!nobind)
				$(win).bind('scroll', onscroll);
			// run lazyload at first
			onscroll();
		});
	}
	
})(jQuery, window);
